package com.hundunjiang.BlogWebServer.server.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.hundunjiang.BlogWebServer.mapper.UserBlogMappper;
import com.hundunjiang.BlogWebServer.server.BlogWebUserService;
import com.hundunjiang.CommonServer.api.CommonResult;
import com.hundunjiang.CommonServer.client.AuthClient;
import com.hundunjiang.CommonServer.constant.AuthConstant;
import com.hundunjiang.CommonServer.domain.UserDTO;
import com.hundunjiang.MbgServer.entity.User;
import com.hundunjiang.MbgServer.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class BlogWebUserServiceImpl implements BlogWebUserService {

    @Autowired
    private UserBlogMappper userBlogMappper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AuthClient  authClient;


    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 邮箱注册
     * @param username 用户名
     * @param password 密码
     * @param phone 手机号
     * @param nickname 昵称
     */
    @Transactional
    @Override
    public boolean regist(String username, String password, String phone, String nickname) {


        User user=new User();
        user.setCreateTime(new Date());
        user.setUsername(username);
        user.setPassword(passwordEncoder.encode(password));
        user.setPhone(phone);
        user.setNickname(nickname);
        user.setStatus(1);
        user.setDelete(1);

        if (userBlogMappper.getUserpass(username)>0){
            return false;
        }else {
            userMapper.insertSelective(user);
            return true;
        }
    }

    /**
     * 判断手机号是否被注册
     * @param phone 手机号
     * @return  true 被注册 false 未被注册
     */
    @Override
    public boolean getPhoneis(String phone) {
        if (userBlogMappper.getPhoneis(phone)>0){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 账号登陆
     * @param username 账户名
     * @return  CommonResult
     */
    @Override
    public CommonResult userlogin(String username, String password) {
        Map<String, String> params = new HashMap<>();
        params.put("client_id", AuthConstant.BLOG_WEB_CLIENT_ID);
        params.put("client_secret","123456");
        params.put("grant_type","password");
        params.put("username",username);
        params.put("password",password);
        return authClient.getAccessToken(params);
    }

    /**
     * 账号登陆
     * @param username 账户名
     * @return  UserDTO  用户信息
     */
    @Override
    public UserDTO loadByUsername(String username) {
        User user=userBlogMappper.loadByUsername(username);
        if(user!=null){
            UserDTO userDto=new UserDTO();
            BeanUtil.copyProperties(user,userDto);
            userDto.setRoles(CollUtil.toList("博客web端用户"));
            return userDto;
        }
        return null;
    }
}
